package com.ja.subclass.controller;

import com.ja.subclass.pojo.SubClassSelect;
import com.ja.subclass.pojo.SubClassVo;
import com.ja.subclass.service.SubService;
import com.ja.util.ResultVo;
import com.ja.util.ResultBuildVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by 11936 on 2019/4/25.
 */

@RestController
@RequestMapping("subClass")
public class SubController {
    @Autowired
    private SubService subService;

    @PostMapping("add")
    @CrossOrigin
    public ResultVo addSubClass(@RequestBody @Valid SubClassVo subClassVo, BindingResult bindingResult){
        if (subClassVo.getStatus()){
            subClassVo.setStatusName(1);
        }else {
            subClassVo.setStatusName(0);
        }
        if(bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(), "500");
        }
        return subService.addSubClass(subClassVo);
    }

    @PostMapping("update")
    @CrossOrigin
    public ResultVo updateSubClass(@RequestBody @Valid SubClassVo subClassVo, BindingResult bindingResult){
        if (subClassVo.getStatus()){
            subClassVo.setStatusName(1);
        }else {
            subClassVo.setStatusName(0);
        }
        if(bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(), "500");
        }
        return subService.updateSubClass(subClassVo);
    }

    @PostMapping("updateStatus")
    @CrossOrigin
    public ResultVo updateStatus(@RequestBody SubClassVo subClassVo){
        if (subClassVo.getStatus()){
            subClassVo.setStatusName(0);
        }else {
            subClassVo.setStatusName(1);
        }
        return subService.updateStatus(subClassVo);
    }

    @PostMapping("queryPage")
    @CrossOrigin
    public ResultVo queryPage(@RequestBody SubClassSelect subClassSelect){
        return subService.qureyPage(subClassSelect);
    }
}
